package com.example.admintor.humaninteraction;

import android.app.Fragment;
import android.content.DialogInterface;
import android.graphics.Color;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import fragment.EightFragment;
import fragment.ElevenFragment;
import fragment.FiveFragment;
import fragment.FourFragment;
import fragment.MusicFragment;
import fragment.NineFragment;
import fragment.OneFragment;
import fragment.SevenFragment;
import fragment.SixFragment;
import fragment.TenFragment;
import fragment.ThreeFragment;
import fragment.TwelveFragement;
import fragment.TwoFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnTouchListener {

    private CoverFlowViewPager mCover;
    RelativeLayout relativeLayout;
    List<View> list = new ArrayList<>();
    int[] small_icon = {
            R.mipmap.music_lititle,
//            R.mipmap.a,
            R.mipmap.c, R.mipmap.e, R.mipmap.f,
//            R.mipmap.b,
            R.mipmap.h,
//            R.mipmap.d,
            R.mipmap.g, R.mipmap.i, R.mipmap.j, R.mipmap.k, R.mipmap.l};

    MusicFragment musicFragment;
    //    OneFragment oneFragment;
    TwoFragment twoFragment;
    ThreeFragment threeFragment;
    FourFragment fourFragment;
    //    FiveFragment fiveFragment;
    SixFragment sixFragment;
    SevenFragment sevenFragment;
    EightFragment eightFragment;
    NineFragment nineFragment;
    TenFragment tenFragment;
    //    ElevenFragment elevenFragment;
    TwelveFragement twelveFragement;
    List<Fragment> listfr = new ArrayList<>();

    ImageView left, right;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        relativeLayout = (RelativeLayout) findViewById(R.id.parent);
        left = (ImageView) findViewById(R.id.left);
        right = (ImageView) findViewById(R.id.right);

        left.setOnClickListener(this);
        right.setOnClickListener(this);
        relativeLayout.setOnTouchListener(this);

        musicFragment = new MusicFragment();
//        oneFragment = new OneFragment();
        twoFragment = new TwoFragment();
        threeFragment = new ThreeFragment();
        fourFragment = new FourFragment();
//        fiveFragment = new FiveFragment();
        sixFragment = new SixFragment();
        sevenFragment = new SevenFragment();
        eightFragment = new EightFragment();
        nineFragment = new NineFragment();
        tenFragment = new TenFragment();
//        elevenFragment = new ElevenFragment();
        twelveFragement = new TwelveFragement();

        listfr.add(musicFragment);
//        listfr.add(oneFragment);
        listfr.add(twoFragment);
        listfr.add(threeFragment);
        listfr.add(fourFragment);
//        listfr.add(fiveFragment);
        listfr.add(sixFragment);
//        listfr.add(elevenFragment);
        listfr.add(sevenFragment);
        listfr.add(eightFragment);
        listfr.add(nineFragment);
        listfr.add(tenFragment);
        listfr.add(twelveFragement);

        getFragmentManager().beginTransaction().add(R.id.parent, sixFragment).commit();
        mCover = (CoverFlowViewPager) findViewById(R.id.cover);
        initMenu();

    }

    int kes = 5;

    public void initMenu() {
        // 初始化数据
        for (int i = 0; i < 10; i++) {
            ImageView img = new ImageView(this);
            img.setBackgroundResource(small_icon[i]);
            list.add(img);
        }
        //设置显示的数据
        mCover.setViewList(list);
        mCover.setCurrentItem(4);
        // 设置滑动的监听，该监听为当前页面滑动到中央时的索引
        mCover.setOnPageSelectListener(new OnPageSelectListener() {
            @Override
            public void select(int position) {
                if (position < kes) {
                    kes = position;
                    getFragmentManager().beginTransaction().setCustomAnimations(R.animator.slide_out_right, R.animator.slide_in_left).replace(R.id.parent, listfr.get(position)).commit();
                } else {
                    kes = position;
                    getFragmentManager().beginTransaction().setCustomAnimations(R.animator.slide_in_lefts, R.animator.slide_out_rights).replace(R.id.parent, listfr.get(position)).commit();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left:
                if (kes > 0) {
                    kes--;
                    mCover.setCurrentItem(kes);
                    getFragmentManager().beginTransaction().setCustomAnimations(R.animator.slide_in_lefts, R.animator.slide_out_rights).replace(R.id.parent, listfr.get(kes)).commit();
                }
                break;
            case R.id.right:
                if (kes < 9) {
                    kes++;
                    mCover.setCurrentItem(kes);
                    getFragmentManager().beginTransaction().setCustomAnimations(R.animator.slide_out_right, R.animator.slide_in_left).replace(R.id.parent, listfr.get(kes)).commit();
                }
                break;
        }
    }

    float starX, endX;

    @Override
    public boolean onTouch(View v, MotionEvent motionEvent) {
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                starX = motionEvent.getX();
                break;
            case MotionEvent.ACTION_UP:
                endX = motionEvent.getX();
                if (endX - starX < 0) {
                    if (Math.abs(endX - starX) > 50) {
                        Log.e("move", "向右");
                        if (kes < 9) {
                            kes++;
                            mCover.setCurrentItem(kes);
                            getFragmentManager().beginTransaction().setCustomAnimations(R.animator.slide_out_right, R.animator.slide_in_left).replace(R.id.parent, listfr.get(kes)).commit();
                        }
                    }
                } else {
                    if (Math.abs(endX - starX) > 50) {
                        Log.e("move", "向左");
                        if (kes > 0) {
                            kes--;
                            mCover.setCurrentItem(kes);
                            getFragmentManager().beginTransaction().setCustomAnimations(R.animator.slide_in_lefts, R.animator.slide_out_rights).replace(R.id.parent, listfr.get(kes)).commit();
                        }
                    }
                }

                break;
        }
        return true;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            new AlertDialog.Builder(this).setTitle("系统提示").setMessage("是否要退出？").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    LoginActivity.landing.exitLogin();
                    SysApplication.getInstance().exit();
                }
            }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            }).show();
        }
        return super.onKeyDown(keyCode, event);
    }
}
